require('./utils/Page')

let $https = require('/utils/request')
wx.$https = $https
wx.baseURL = $https.baseURL
wx.get = $https.get
wx.put = $https.put
wx.post = $https.post
wx.delete = $https.delete

App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        wx.get('http://localhost:3000/wx/login?code=' + res.code).then(res => {
          wx.userInfo = res
          wx.token = res.token
          if(wx.waitLoginPages.length){
            for(let i = 0 ; i < wx.waitLoginPages.length ; i++) {
              let page = wx.waitLoginPages[i]
              page.onLoad.call(page.context,page.options)
            }
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null
  }
})

/*
var students = [
  {
    name : '张三',
    no : '201901'
  },
  {
    name : '李四',
    no : '201902'
  },
  {
    name : '王五',
    no : '201903'
  }
]

// 方法一
function findStudentByNo1(no){
  for(let i=0;i<students.length;i++){
    if(students[i].no==no){
      return students[i].name
    }
  }
}


// 方法二
var obj={};
  students.map(function(e,item){
    obj[e.name] =e.no
  })

  function findStudentByNo2 (value, compare = (a, b) => a === b) {
    return Object.keys(obj).find(k => compare(obj[k], value))
    }

console.log(findStudentByNo1('201901'));
console.log(findStudentByNo2('201902'));

// 1 0-99的随机数
function random1(){
  return (Math.floor(Math.random()*100-1))
}

// 2 99-999
function random2(){
  return Math.floor(Math.random()*900)+99
}

console.log(random1());
console.log(random2());
*/

/*
//作业1
function dateFormat(date,formatString){
  let year = date.getFullYear()
  formatString = formatString.replace(/(y+)/i,function($1){
    return year.toString().substr(4 - $1.length)
  })
  let month = date.getMonth() + 1
  formatString = formatString.replace(/(m+)/i,function(){
    return month.toString()<10?'0'+month.toString() : month.toString()
  })
  let day = date.getDate()
  formatString = formatString.replace(/(d+)/i,function(){
    return day.toString()<10?'0' + day.toString() : day.toString()
  })
  let hour = date.getHours()
  formatString = formatString.replace(/(h+)/i,function(){
    return hour.toString()<10?'0' + hour.toString() : hour.toString()
  })
  let minute = date.getMinutes()
  formatString = formatString.replace(/(m+)/i,function(){
    return minute.toString()<10?'0' + minute.toString() : minute.toString()
  })
  let second = date.getSeconds()
  formatString = formatString.replace(/(s+)/i,function(){
    return second.toString()<10?'0' + second : second.toString()
  })
  return formatString
}
let date = new Date()
date.setFullYear(2023)
date.setMonth(3)
date.setDate(17)
date.setHours(20)
date.setMinutes(7)
date.setSeconds(13)
// 'YYYY-MM-DD hh:mm:ss'
console.log(dateFormat(date,'YYYY年mm月dd日 hh:mm:ss'));


// 作业二
//按分页获取数组中的数据
//array 数组
//pageNum 开始页从1开始，默认1
//pageSize 每页大小，默认为10
//返回结果示例{total：10，rows：[]} total为数据总数，rows为具体返回的数据

// [1,2,3,4,5,6,7,8] pageSize=5 pageNum=1
// {total:8,rows[1,2,3,4,5]}
function getList(array,pageNum,pageSize){
  let total =array.length;
  let rows =[];
  // 完成分页的过程
  if(pageNum == undefined){
    pageNum = 1;
  }
  if(pageSize == undefined){
    pageSize = 10
  }
  for(let i=(pageNum-1)*pageSize,k=0;k<pageSize&&array[i]!=undefined;k++,i++){
    rows[k] = array[i];
  }
//  返回结果示例{total：10，rows：[]} total为数据总数，rows为具体返回的数据
  return '{total:'+total+',rows:['+rows+']}';
}

function bulidArray(){
  var rc = [];
  for(let i=0;i<Math.floor(Math.random()*100-1);i++){
  rc[rc.length] = Math.floor(Math.random()*100-1);
  }
  return rc;
}

var array = bulidArray()
console.log(array);
console.log(getList(array,3,3));
 

var text = '{ "employees" : [' +
'{ "firstName":"Bill" , "lastName":"Gates" },' +
'{ "firstName":"Steve" , "lastName":"Jobs" },' +
'{ "firstName":"Alan" , "lastName":"Turing" } ]}';

var obj = JSON.parse(text);
console.log(obj.employees.length);

function Car(name,color){
  this.name = name;
  this.color = color;
}Car.prototype.age = 100;

let car1 = new Car('宝马','black')
let car2 = new Car('奔驰','white') 
console.log(car1);
console.log(car2);
*/

const utils = require('/utils/util')
// console.log(utils);